<template>
  <el-dialog v-model="dialogUserVisible" title='编辑用户信息' width="600px" align-center draggable>
    <div class="dialog-content">
      <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="100px">
				<el-form-item label="账号" prop="account">
					<el-input v-model="formData.account" disabled />
				</el-form-item>
				<el-form-item label="姓名" prop="name">
					<el-input v-model="formData.name" />
				</el-form-item>
				<el-form-item label="性别" prop="sex">
					<el-select v-model="formData.sex" placeholder="请选择性别">
						<el-option label="男" value="男" />
						<el-option label="女" value="女" />
					</el-select>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="formData.email" />
				</el-form-item>
				<el-form-item label="部门" prop="department">
					<el-select v-model="formData.department" placeholder="请选择部门">
						<el-option v-for="item in departmentData" :key="item" :label="item" :value="item" />
					</el-select>
				</el-form-item>
			</el-form>
    </div>
    <template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="editUser">确定</el-button>
			</span>
		</template>
  </el-dialog>
</template>
 
<script setup lang="ts">
  import { ref, onBeforeUnmount, reactive } from 'vue';
  import { ElMessage } from 'element-plus';
  import { bus } from "@/utils/mitt.js";
  import { getUserInfo, editAdmin } from '@/api/userinfor.js';
	import { getDepartment } from '@/api/setting.js';
  
  interface FormData {
		id ?: number,
		account : number,
		name : string,
		sex : string,
		email : string,
		department : string
	}
	const formData:FormData = reactive({
		account: null,
		name: '',
		sex: '',
		email: '',
		department: '',
		identity: '用户',
	})
	const rules = reactive({
		account: [
			// 失去焦点触发 trigger: 'blur'
			{ required: true, message: '请输入管理员的注册账号', trigger: 'blur' },
			{ pattern: /^[0-9]{1,10}$/, message:"请输入管理员的注册账号 1-10", trigger: 'blur' },
			
		],
		password: [
			{ required: true, message: '请输入管理员的注册密码', trigger: 'blur' },
			{ pattern: /^(?=.*[a-z])(?!\d+$)[a-z\d]{6,12}$/, message:"密码至少带一个小写字母，长度6-12", trigger: 'blur' },
		],
		name: [
			{ required: true, message: '请输入管理员的名字', trigger: 'blur' },
		],
		sex: [
			{ required: true, message: '请输入管理员的性别', trigger: 'blur' },
		],
		email: [
			{ required: true, message: '请输入管理员的邮箱', trigger: 'blur' },
			{ pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/, message:"请输入管理员的邮箱", trigger: 'blur' },
		],
		department: [
			{ required: true, message: '请输入管理员的部门', trigger: 'blur' },
		],
	})

  // 部门数据
	const departmentData = ref([])
	const returnDepartment = async() => {
		const res = await getDepartment()
		departmentData.value = res.data
	}
	returnDepartment()

  bus.on('editId', async (id : number) => {
		const res = await getUserInfo(id)
		formData.id = res.data.id
		formData.account = res.data.account
		formData.name = res.data.name
		formData.sex = res.data.sex
		formData.email = res.data.email
		formData.department = res.data.department
	})

  const editUser = async () => {
    const res = await editAdmin(formData)
		if (res.data.status == 0) {
			ElMessage({
				message: '编辑用户信息成功',
				type: 'success',
			})
			bus.emit('offDialog',1)  
			dialogUserVisible.value = false
		} else {
			ElMessage.error('编辑用户信息失败')
			dialogUserVisible.value = false
		}
  }

  // ------------------------------------------------------------------------------
  // 弹窗开关
	const dialogUserVisible = ref(false) 
  const open = () => {
    dialogUserVisible.value = true
  }
  defineExpose({
    open
  })
  onBeforeUnmount(()=>{
    bus.all.clear()
  })
</script>
 
<style lang="scss" scoped>
	.dialog-content {
		display: flex;
		padding: 20px 30px;
	}

	:deep(.el-form-item) {
		margin: 30px;
	}
</style>
